package com.link.base.base.appinfo.service;

import com.link.base.base.appinfo.dao.mybatis.mapper.AppLocLogMapper;
import com.link.base.base.appinfo.model.AppKeyInfo;
import com.link.base.base.appinfo.model.AppLocLog;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.cllog.LCLogger;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 前端日志
 *
 * @author ouyangzhenqiang
 * @date 2018/10/16
 */
@Service
public class AppLocLogServiceImpl extends BasicServiceImpl<AppLocLog> implements AppLocLogService {

    @Resource
    private AppLocLogMapper appLocLogMapper;

    @Override
    public BasicMapper<AppLocLog> getBasicMapper() {
        return appLocLogMapper;
    }

    @Override
    public void collectKeyInfo(AppKeyInfo record) throws Exception {
        CoreUser user = UserUtil.getUser();
        record.setId(keyGenerateService.keyGenerate());
        record.setUserId(user.getId());
        record.setLogin(user.getUsername());
        appLocLogMapper.appKeyInfoInsert(record);
    }

    @Override
    public void beforInsert(AppLocLog record) {
        //日志表自增，无需手动设置id，此处设置为了防止父类生成没必要的id
        record.setId(0L);
        //记录数据到日志系统
        //警告错误
        String warn = "warn";
        //错误
        String error = "error";
        if (warn.equalsIgnoreCase(record.getLogLevel())) {
            LCLogger.warn().withMessageKey("appLocLog").withMessage("记录前端定时提交的关键信息: " + record.getLocalLogInfo()).flush();
        } else if (error.equalsIgnoreCase(record.getLogLevel())) {
            LCLogger.error().withMessageKey("appLocLog").withMessage("记录前端定时提交的关键信息: " + record.getLocalLogInfo()).flush();
        } else {
            LCLogger.info().withMessageKey("appLocLog").withMessage("记录前端定时提交的关键信息: " + record.getLocalLogInfo()).flush();
        }
    }
}
